package com.example.ui.register;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.NetRequest;
import com.example.ui.IndexActivity;
import com.example.ui.LoginActivity;
import com.example.ui.learn.CurriculumCenterActivity;
import com.example.ui.share.IndustryFragment;
import com.example.util.Util;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class R5Fragment extends Fragment {
	private ImageView ra5_t1_clear, ra5_t2_clear;
	private RelativeLayout ra5_l_industry;
	private static TextView ra5_txt_industry;
	private TextView ra5_title;
	private Button ra5_next, ra5_return;
	public static String zhiwu="",company="",res="";	
	List<String> list;
	 private String name;
	
	public EditText ra5_company, ra5_zhiwu;
	AlertDialog infoad;
	AlertDialog.Builder bulider;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.r5fragment, null);
		
		ra5_title = (TextView) v.findViewById(R.id.register_title);
		ra5_next = (Button) v.findViewById(R.id.register_next);
		ra5_return = (Button) v.findViewById(R.id.register_return);
		ra5_next.setText("完成");
		ra5_title.setText("企业信息(5/5)");
		
		ra5_t1_clear = (ImageView) v.findViewById(R.id.ar5_company_clear);
		ra5_t2_clear = (ImageView) v.findViewById(R.id.ar5_zhiwu_clear);
		ra5_company = (EditText) v.findViewById(R.id.ar5_company);
		ra5_zhiwu = (EditText) v.findViewById(R.id.ar5_zhiwu);
		ra5_l_industry = (RelativeLayout) v.findViewById(R.id.ar5_l_industry);
		ra5_txt_industry = (TextView) v.findViewById(R.id.ar5_btn_industry);
		
		ra5_t1_clear.setOnClickListener(ra5_ocl);
		ra5_t2_clear.setOnClickListener(ra5_ocl);
		ra5_l_industry.setOnClickListener(ra5_ocl);
		ra5_next.setOnClickListener(ra5_ocl);
		ra5_return.setOnClickListener(ra5_ocl);
		
		
		return v;
	}

	
	  public static Handler h=new Handler(){
		 public void handleMessage(android.os.Message msg) {
			 switch (msg.what) {
			case 1:
				if (!IndustryFragment.ind.equals("")) {
					ra5_txt_industry.setText(IndustryFragment.ind);
				}
				break;
			

			default:
				break;
			}
		 };
	 };
	private void next() {			
			company=ra5_company.getText().toString();
			zhiwu=ra5_zhiwu.getText().toString();
			
		if(!company.equals("")){
		if(!IndustryFragment.ind.equals("")){
			if(!zhiwu.equals("")){
				list=new ArrayList<String>();
				name=RegisterActivity4.name.replace(" ","%20");				
				company=company.replace(" ", "%20");
				zhiwu=zhiwu.replace(" ", "%20");
				list.add(name);
				list.add(RegisterActivity4.g);
				list.add(RegisterActivity4.province);
				list.add(RegisterActivity4.city);
				list.add(company);
				list.add(zhiwu);
				list.add(IndustryFragment.ind);	
				bulider = new AlertDialog.Builder(getActivity());
				bulider.setView(View.inflate(getActivity(), R.layout.onloading, null))
						.create();
				infoad = bulider.show();
				 new Thread(r5_run).start();																				

		} else {
			Util.toast(getActivity(), "请填写完整信息");
		}
		}

	}
	}
		Runnable r5_run = new Runnable() {
			@Override
			public void run() {		
				res=NetRequest.postRequest(list);
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(res);
					String response = jsonObject.getString("response");	
					infoad.dismiss();
					if (response.equals("true")) {	
						handler.sendEmptyMessage(1);									
					}else{
						handler.sendEmptyMessage(2);
					}
				} catch (Exception e) {					
					e.printStackTrace();
				}									
			}
		};
		Handler handler=new Handler(){
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {
				case 1:
					new AlertDialog.Builder(getActivity()).setTitle("恭喜你。注册成功！").setMessage("快去登录吧！")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent ra5_i_next=new Intent();
							ra5_i_next.setClass(getActivity(),IndexActivity.class);
							startActivity(ra5_i_next);
							IndexActivity.isRegister = true;
							getActivity().finish();	
						}
					}).show();
					break;
				case 2:
					//new AlertDialog.Builder(getActivity()).setTitle("提示").setMessage("").show();
					new AlertDialog.Builder(getActivity()).setTitle("很抱歉。注册失败！").setMessage("请重新注册！")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent ra5_i_next=new Intent();
							ra5_i_next.setClass(getActivity(),IndexActivity.class);
							startActivity(ra5_i_next);
							IndexActivity.isRegister = false;
							getActivity().finish();	
						}
					}).show();
					break;

				default:
					break;
				}
			};
		};

	OnClickListener ra5_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.register_next:
				next();
				break;
			case R.id.register_return:
				getActivity().onBackPressed();
				break;

			case R.id.ar5_company_clear:
				ra5_company.setText("");
				break;
			case R.id.ar5_zhiwu_clear:
				ra5_zhiwu.setText("");
				break;

			case R.id.ar5_l_industry:
				IndustryFragment.type=1;
				Intent i=new Intent();
				i.setClass(getActivity(), SelectIndustryFragment.class);
				startActivity(i);				
				break;

			default:
				break;
			}
		}
	};
	

}
